//
//  LPToast.m
//  LesPark
//
//  Created by zhudf on 15/11/19.
//
//

#import "LPToast.h"

static CGFloat kDuration = 2.0;

@implementation LPToast

+ (void)toast:(NSString *)text {
    [self toast:text duration:kDuration];
}

+ (void)toast:(NSString *)text duration:(NSTimeInterval)duration {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self toast:text inView:UIApplication.sharedApplication.keyWindow duration:duration];
    });
}

+ (void)toast:(NSString *)text inView:(UIView *)view{
    [self toast:text inView:view duration:kDuration];
}

+ (void)toast:(NSString *)text inView:(UIView *)view duration:(NSTimeInterval)duration{
    [self showText:text inView:view hideDelay:duration];
}

+ (void)showText:(NSString *)text inView:(UIView *)view{
    [self showText:text inView:view hideDelay:kDuration];
}

+ (void)showText:(NSString *)text inView:(UIView *)view hideDelay:(NSTimeInterval)hideDelay{
    
    if (!view || !text) {
        return;
    }
    
    if ([NSThread isMainThread]) {
        MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
        if (!hud) {
            hud = [[MBProgressHUD alloc] initWithView:view];
            [view addSubview:hud];
        }
        [view bringSubviewToFront:hud];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = nil;
        hud.detailsLabel.text = text;
        
        hud.userInteractionEnabled = NO;
        
        [hud showAnimated:YES];
        [hud hideAnimated:YES afterDelay:hideDelay];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self showText:text inView:view hideDelay:hideDelay];
        });
    }
}

@end
